package View;

// Autoren: Oliver Schewe, Oleg Zender, Malte Rudolph

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JProgressBar;

import Model.Client;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.DefaultListModel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JCheckBox;
import javax.swing.JTabbedPane;

import javax.swing.JList;
import javax.swing.JScrollPane;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

public class Gui extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private Client c;
	private JProgressBar pbFullDownload, pbDownload;
	private JLabel lblState;
	private JTextField txtField;
	private JLabel lblDownload;
	private JTextField txtDir;
	private JCheckBox cbNewFolder;
	private JTextField txtServerIp;
	private JTabbedPane tabbedPane;
	private DefaultListModel listModelUpdate, listModelDownload, listModelDelete;
	private JList listUpload;
	private JList listDownload;
	private JList listDelete;
	private Properties prop = new Properties();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		
		
		
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Gui frame = new Gui();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	public Gui() throws ClassNotFoundException, IOException {
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				prop.clear();
				prop.put("serverip", txtServerIp.getText());
				prop.put("directory", txtDir.getText());
				try {
					prop.store(new FileOutputStream(System.getProperty("user.home") + "\\dropboxclone.ini"), "DropboxClone properties");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});
		
		File f = new File(System.getProperty("user.home") + "\\dropboxclone.ini");
		if(f.exists())
			prop.load(new FileInputStream(System.getProperty("user.home") + "\\dropboxclone.ini"));
		
		setTitle("DropboxClone");
		setResizable(false);		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 386);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		lblDownload = new JLabel("-");
		lblDownload.setBounds(10, 71, 84, 14);
		contentPane.add(lblDownload);
		
		pbDownload = new JProgressBar();
		pbDownload.setBounds(10, 85, 424, 23);
		pbDownload.setStringPainted(true);
		pbDownload.setMaximum(100);
		contentPane.add(pbDownload);
		
		pbFullDownload = new JProgressBar();
		pbFullDownload.setBounds(10, 111, 424, 7);
		pbFullDownload.setMaximum(1000);
		contentPane.add(pbFullDownload);
		
		txtField = new JTextField();
		txtField.setToolTipText("Current state: Idle, Uploading or Downloading");
		txtField.setText("Idle");
		txtField.setEditable(false);
		txtField.setBounds(42, 11, 98, 20);
		contentPane.add(txtField);
		txtField.setColumns(10);
		
		lblState = new JLabel("State");
		lblState.setBounds(10, 14, 46, 14);
		contentPane.add(lblState);		
		
		cbNewFolder = new JCheckBox("Change directory");
		cbNewFolder.setToolTipText("Check if you changed the path of directory to be synchronized!");
		cbNewFolder.setBounds(312, 35, 126, 23);
		contentPane.add(cbNewFolder);
		
		
		
		txtDir = new JTextField();
		txtDir.setToolTipText("Enter path of directory to be synchronize!");
		if(prop.get("directory") != null)
			txtDir.setText((String) prop.get("directory"));
		else
			txtDir.setText("D:\\sync\\");
		txtDir.setBounds(229, 11, 205, 20);
		contentPane.add(txtDir);
		txtDir.setColumns(10);
		
		JLabel lblDirectory = new JLabel("Directory");
		lblDirectory.setBounds(175, 14, 55, 14);
		contentPane.add(lblDirectory);
		
		JLabel lblIp = new JLabel("IP");
		lblIp.setBounds(10, 46, 46, 14);
		contentPane.add(lblIp);
		
		txtServerIp = new JTextField();
		if(prop.get("serverip") != null)
			txtServerIp.setText((String) prop.get("serverip"));
		else
			txtServerIp.setText("localhost");
		txtServerIp.setBounds(42, 42, 98, 20);
		contentPane.add(txtServerIp);
		txtServerIp.setColumns(10);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		tabbedPane.setBounds(10, 183, 424, 164);
		contentPane.add(tabbedPane);
		
		JScrollPane scrollPaneUpload = new JScrollPane();
		tabbedPane.addTab("Uploaded", null, scrollPaneUpload, null);
		
		listModelUpdate = new DefaultListModel<>();
		listUpload = new JList(listModelUpdate);
		scrollPaneUpload.setViewportView(listUpload);
		
		JScrollPane scrollPaneDownload = new JScrollPane();
		tabbedPane.addTab("Downloaded", null, scrollPaneDownload, null);
		
		listModelDownload = new DefaultListModel<>();
		listDownload = new JList(listModelDownload);
		scrollPaneDownload.setViewportView(listDownload);
		
		JScrollPane scrollPaneDeleted = new JScrollPane();
		tabbedPane.addTab("Deleted", null, scrollPaneDeleted, null);
		
		listModelDelete = new DefaultListModel<>();
		listDelete = new JList(listModelDelete);
		scrollPaneDeleted.setViewportView(listDelete);
		
				
		JButton btnNewButton = new JButton("Synchronize");
		btnNewButton.setToolTipText("Click to synchronize with server!");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				c = new Client();
				pbFullDownload.setValue(0);
				pbFullDownload.setString("");
				pbDownload.setValue(0);
				pbDownload.setString("");
				txtField.setText("Idle");
				try {
					String dir = txtDir.getText();
					String ServerIp = txtServerIp.getText();
					boolean newDir = cbNewFolder.isSelected();					
					tabbedPane.setTitleAt(0, "Uploaded");
					tabbedPane.setTitleAt(1, "Downloaded");
					tabbedPane.setTitleAt(2, "Deleted");
					
					listModelUpdate.clear();
					listModelDownload.clear();
					listModelDelete.clear();
					
					c.init(pbFullDownload, pbDownload, txtField, lblDownload, dir, newDir, ServerIp, listModelUpdate, listModelDownload, listModelDelete, tabbedPane);
					c.startTransfer();
				} catch (ClassNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				txtField.setText("Idle");
				cbNewFolder.setSelected(false);
				lblDownload.setText("-");
			}
		});
		btnNewButton.setBounds(131, 140, 154, 23);
		contentPane.add(btnNewButton);
		
		this.setVisible(true);
		
	}
}
